package com.lenovo.smarttraffic.zm;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.zm.MyRoute;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class DetailsRouteActivity extends BaseActivity implements Serializable {
    String TAG = "DetailsRouteActivity";

    private TextView startPlace;
    private ListView RoutePlaceListView;
    private TextView endPlace;
    private List<MyRoute> detailsRoute;
    private TextView tv_startPlace;
    private TextView tv_endPlace;
    private TextView tv_time;



    private TextView tv_distance;
    private float time;
    private float distance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        //内部类不能Serializable传值
        detailsRoute = (List<MyRoute>) intent.getSerializableExtra("DetailsRoute");
        time = intent.getFloatExtra("time", 0);
        distance = intent.getFloatExtra("distance", 0);
        Log.e(TAG, "initDetailsRoute: " + time + "==" + distance);

        initView();

    }

    private void initView() {
        initToolBar(findViewById(R.id.toolbar), true, "路线指南");
//        沿主路行驶
//        菜户营南路
//        沿菜户营南路向南行驶863米靠左沿主路行驶
        //  startPlace = (TextView) findViewById(R.id.startPlace);
        RoutePlaceListView = (ListView) findViewById(R.id.RoutePlace);
        ZmListItemAdapter adapter = new ZmListItemAdapter(this, detailsRoute);
        RoutePlaceListView.setAdapter(adapter);
        //   endPlace = (TextView) findViewById(R.id.endPlace);
        tv_startPlace = (TextView) findViewById(R.id.tv_startPlace111);

        tv_endPlace = (TextView) findViewById(R.id.tv_endPlace);

        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_distance = (TextView) findViewById(R.id.tv_distance);


        tv_startPlace.setText(detailsRoute.get(0).getRoad);

        tv_endPlace.setText(detailsRoute.get((detailsRoute.size() - 1)).getRoad);
        tv_time.setText((int) time / 60 + "分钟");
        tv_distance.setText((int) distance /1000 + "公里");
    }

    @Override
    protected int getLayout() {
        return (R.layout.activity_details_route);
    }


    public class ZmListItemAdapter extends BaseAdapter {

        private List<MyRoute> objects = new ArrayList<MyRoute>();

        private Context context;
        private LayoutInflater layoutInflater;

        public ZmListItemAdapter(Context context, List<MyRoute> detailsRoute) {
            this.context = context;
            this.layoutInflater = LayoutInflater.from(context);
            this.objects = detailsRoute;
        }

        @Override
        public int getCount() {
            return objects.size();
        }

        @Override
        public MyRoute getItem(int position) {
            return objects.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.zm__list_item, null);
                convertView.setTag(new ViewHolder(convertView));
            }
            initializeViews((MyRoute) getItem(position), (ViewHolder) convertView.getTag());
            if (position == 0) {
                Log.e(TAG, "getView: " + position);
//                            Drawable drawable = getResources().getDrawable(R.drawable.ic_et_bg);
                ((ViewHolder) convertView.getTag()).View_item.setVisibility(View.GONE);
                ((ViewHolder) convertView.getTag()).tvRoad.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.mipmap.start), null, null, null);
            }
            if (position == (objects.size() - 1)) {
                Log.e(TAG, "getView: " + position);
                ((ViewHolder) convertView.getTag()).tvRoad.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.mipmap.end), null, null, null);
                ((ViewHolder) convertView.getTag()).View_item.setVisibility(View.GONE);
            }

            return convertView;
        }

        private void initializeViews(MyRoute object, ViewHolder holder) {

            holder.tvAssistantAction.setText(object.getAssistantAction);
            holder.tvRoad.setText(object.getRoad);
            holder.tvInstruction.setText(object.getInstruction);

        }

        protected class ViewHolder {
            private TextView tvAssistantAction;
            private TextView tvRoad;
            private TextView tvInstruction;
            private View View_item;

            public ViewHolder(View view) {
                tvAssistantAction = (TextView) view.findViewById(R.id.tv_AssistantAction);
                tvRoad = (TextView) view.findViewById(R.id.tv_Road);
                tvInstruction = (TextView) view.findViewById(R.id.tv_Instruction);
                View_item = view.findViewById(R.id.View_item);
            }
        }
    }

}
